package edu.nuc.ahm.juc.procon;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Logger;

/**
 *
 * A -- B  -- C  依次次调用
 */
public class C {
    public static void main(String[] args) {
        Data3 data = new Data3();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                    data.printA();

            }
        },"A").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                data.printB();

            }
        },"B").start();

        new Thread(()->{
            for (int i = 0; i < 10; i++) {

                data.printC();

            }
        },"C").start();


    }



}

class Data3{  // 数字资源类
   private Lock lock = new ReentrantLock();
   private Condition condition1 = lock.newCondition();
   private Condition condition2= lock.newCondition();
   private Condition condition3 = lock.newCondition();
   private int number = 1;// 1A  2B  3C
   public void printA(){
       lock.lock();
       try {
           // 判断业务 执行  通知
           while (number!=1){
               // 等待
               condition1.await();
           }
           System.out.println(Thread.currentThread().getName()+"AAAAAAAA");
           number = 2;
           // 唤醒 指定的人 B
           condition2.signal();
       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           lock.unlock();
       }
   }
    public void printB(){
       lock.lock();
        try {
            // 判断业务 执行  通知
            while (number!=2){
                // 等待
                condition2.await();
            }
            System.out.println(Thread.currentThread().getName()+"==="+"BBBBBBB");
            number = 3;
            // 唤醒 指定的人 B
            condition3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }


    }
    public void printC(){
        lock.lock();
        try {
            // 判断业务 执行  通知
            while (number!=3){
                // 等待
                condition3.await();
            }
            System.out.println(Thread.currentThread().getName()+"CCCCCCCCCC");
            number = 1;
            condition1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }


}